<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 class Userobj_model extends CI_Model
 {
     public $uname;
     public $upass;
     public $urealname;

     public function __construct()
     {
         parent::__construct();
         $this->load->database();
     }
     public function inset_user()
     {
        return $this->db->insert('user',$this);
     }

     public function Get_UserList_Page()
     {

     	return $this->db->select()->from('user')->where('udi>0')->get()->result();
     }

     public function Get_UserBy_Id($uid)
     {
     	return $this->db->select()->from('user')->where(array('udi'=>$uid))->get()->row();
     }
     public function EditUserBy($userModel)
     {
          $this->db->update('user',$userModel,array('udi'=>$userModel->udi));
          return $this->db->affected_rows();
     }

     public function DelUser_ById($uid)
     {
         $this->db->delete('user',array('udi'=>$uid));
         return $this->db->affected_rows();
     }

     public function GetUserBy_Name($userModel)
     {
         return $this->db->select()->from('user')->where(array('uname'=>$userModel->uname,'upass'=>$userModel->upass))->get()->result();
     }

     public function CheckAddUserName($val)
     {
         $res= $this->db->select()->from('user')->where(array('uname'=>$val))->get()->row();
         if(empty($res))
         {
             return 1;
         }else{
             return 2;
         }
     }
     public function CheckEditUserName($val)
     {
         $res= $this->db->select()->from('user')->where(array('uname'=>$val))->get()->result_array();
         if(empty($res) || count($res) <=1)
         {
             return 1;
         }else{
             return 2;
         }
     }

     public function BatchDel_User($udiArray)
     {
         $this->db->trans_begin();
         foreach($udiArray as $udiitem)
         {
             $this->db->delete('user',array('udi'=>$udiitem));
         }
         if ($this->db->trans_status() === FALSE)
         {
             $this->db->trans_rollback();
             return 0;
         }
         else
         {
             $this->db->trans_commit();
             return 1;
         }
     }
 }
?>